package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.AnchorActivity;
import com.xgkj.diyiketang.activity.SearchAnchorActivity;
import com.xgkj.diyiketang.activity.data.PaiHangAdatper;
import com.xgkj.diyiketang.activity.data.XingXingBangAdatper;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.XingXingBangTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class Fragment_XingXing extends BaseFragment implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
	private Context mContext;
	private PullToRefreshListView xxListView;
	private XingXingBangAdatper xxAdapter;
	private int xingxingTaskId = -1;
	private int currentPage = 1;
	private ImageView sousuo;

	@Override
	protected int getContentViewId() {
		return 0;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_xingxing, null);
		mContext = getActivity();
		initView(view);
		return view;
	}

	private void initView(View view){
		sousuo = (ImageView) view.findViewById(R.id.xingxing_search);
		sousuo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivity(new Intent(mContext, SearchAnchorActivity.class));
			}
		});

		xxListView = (PullToRefreshListView)view.findViewById(R.id.xingxing_list);
		xxListView.init(mContext,true);
		xxAdapter = new XingXingBangAdatper(mContext);

		xxListView.setAdapter(xxAdapter,true);
		xxListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				if(position==0){
				if(xxListView.getHeaderViewsCount()>0){
					position--;
				}

				if (position < 0 || position>=xxAdapter.getCount()) return;
					EatShowUserInfo info = (EatShowUserInfo) xxAdapter.getItem(position);
					startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
//				}else{
//					EatShowUserInfo info = (EatShowUserInfo) xxAdapter.getItem(position-1);
//					startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
//				}

			}
		});
		xxListView.setOnRefreshListener(this);
		xxListView.setOnLoadDataListener(this);
		xxListView.doRefresh();

	}

	private  void getXingXingBang(){
		if(xingxingTaskId != -1){
			return;
		}
		if(currentPage == 0){
			xxListView.setNoNextPage();
			return;
		}

		XingXingBangTask task = new XingXingBangTask();
		task.addListener(new XingXingListener(mContext));
		xingxingTaskId = getEatShowApp().getTaskManager().addTask(task);
	}

	class XingXingListener extends ShowToastTaskEventListener {
		public XingXingListener(Context context) {
			super(context);
		}

		@Override
		public void handleTaskCompleted(int taskId, int result, Object data) {
			super.handleTaskCompleted(taskId, result, data);
			if(taskId == xingxingTaskId){
				xingxingTaskId = -1;
				if(currentPage == 1){
					xxAdapter.clearData();
					xxListView.onRefreshComplete();
					xxListView.enableLoadMore();
				}else{
					xxListView.onLoadDataComplete(true);
				}
				List<EatShowUserInfo> fansList = new ArrayList<EatShowUserInfo>();
				if(data != null){
					fansList = (List<EatShowUserInfo>) data;

				}

				if(fansList != null && fansList.size()>0){
					xxAdapter.updateData(fansList);
				}

				currentPage = result;

			}
		}

		@Override
		public void handleTaskError(int taskId, int errorCode, String errorMsg) {
			super.handleTaskError(taskId, errorCode, errorMsg);
			if(taskId == xingxingTaskId){
				xingxingTaskId = -1;
				if(currentPage == 1){
					xxListView.onRefreshComplete();
				}else{
					xxListView.onLoadDataComplete(false);
				}
			}
		}
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if(!hidden){
			//mListView.measure(0,0);
			if(xxAdapter != null && xxAdapter.getCount() == 0){
				xxListView.doRefresh();
			}
		}
	}
	@Override
	public void onPullRefresh() {

	}

	@Override
	public void onLoadMore() {
		getXingXingBang();
	}

	@Override
	public void onRefresh() {
		currentPage=1;

		getXingXingBang();
//		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position-1);
//				startActivity(AnchorActivity.getIntent(mContext, info.getUserId()));
//			}
//		});

	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("XingxingFragment");
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("XingxingFragment");
	}

}
